<?php echo $this->render('link/header.phtml') ?>
//常用变量初始化
var menuDelUrl=baseUrl+"/link/serverclient/type/del";
var menuEditUrl=baseUrl+"/link/serverclient/type/edit";
var module="update.index";
var gridTitle="产品列表";
var dataId="product_id";
//Ext.Msg.show({
//	title:'请谨慎操作',
//    msg:'<span style="color:#033;font-size:14px;font-weight:bold">由于操作可能存在的问题，请谨慎操作，谢谢</span>',
//    buttons:Ext.Msg.OK,
//    icon:Ext.MessageBox.INFO,
//    animEl:'message'
//});

var Product = Ext.data.Record.create([
           {name: 'product_id', type: 'int',mapping:'entity_id'},
           {name: 'name', type: 'string'},
		   {name: 'price', type: 'string'},
           {name: 'url_path', type: 'string'},
           {name: 'images', type: 'string',mapping:'image'},
           {name: 'weight', type: 'string'},
           {name: 'url_key', type: 'float'},
           {name:'created_at'}
      ]);

var site_combo_store=new Ext.data.JsonStore({
		url:baseUrl+'/site/productdata/',
		root:'results',
		fields:['product_id','name','price','url_path','images','weight','url_key','created_at']

});
var siteData=[
			['8001','www.luxcellphone.com'],
            ['8002','www.myluxphone.com'],
            ['8003','www.myreplicaphone.com'],
            ['8004','www.luxphoneworld.com'],
            ['7001','www.luxphoneonline.com'],
            ['7002','www.onlineluxphone.com']
];
var site_combo_store=new Ext.data.SimpleStore({
	data:siteData,
    fields:['id','name']
});
var site_combo=new Ext.form.ComboBox({
	 									fieldLabel:'选择目标站点',
                                        width:250,
										store:site_combo_store,										
        								displayField:'name',
        								valueField :'id',
										hiddenName:"id",
                                        id:'site_id',
        								typeAhead:true,                                                                         
        								mode:'local',
        								triggerAction:'all',
                                        editable:false,
                                        allowBlank:false,  
        								emptyText:'请选择...',
        								selectOnFocus:true
});

var ds=new Ext.data.Store({
							  proxy:new Ext.data.HttpProxy({
									url:baseUrl+'/update/serverproduct/id/5001'
									}),
							  reader:new Ext.data.JsonReader({
									root:'results',
									totalProperty:'total',
									id:'product_id'
									},                                    
                                    Product)
	});	

//定义列模型
var cm=new Ext.grid.ColumnModel([	
							new Ext.grid.CheckboxSelectionModel(),
                            {header:'编号',dataIndex:'product_id',width:60},
							{header:'产品名',dataIndex:'name',width:100},
							{header:'价格',dataIndex:'price',width:100,renderer:show_money},
							{header:'链接',dataIndex:'url_path',width:150,renderer:show_link},
							{header:'图片',dataIndex:'images',width:100,renderer:show_img},
                            {header:'创建时间',dataIndex:'created_at',width:100,hidden:true}				
	]);
cm.defaultSortable = true; 



//定义工具栏								 
var tb=new Ext.Toolbar({														
							items:[
                            	{xtype:'panel',
                            	 width:600,
                                 layout:'form',
                                 border:false,
                                 height:120,
                                 labelwidth:100,
                                 bodyStyle:'padding:10px',
                                 buttonAlign:'left',
                            	 items:[                            	
                                site_combo,{
                                xtype:'textfield',
                                fieldLabel:'输入ID号',                                
                                name:'product_id',
                                id:'product_id',
                                width:450,
                                allowBlank:false
                                },                                
                                {
                                xtype:'radiogroup',
                                width:300,
                                bodyStyle:'margin:20px',
                                fieldLabel:'选择操作类型',
                                border:false,                                
                                column:2,
                                items:[
                                	{boxLabel:'常规',name:'type',inputValue:'0',id:'type'},
                                    {boxLabel:'范围',name:'type',inputValue:'1',checked:true}
                                ]                              
                                }],
                                bbar:[{
                                minWidth:80,
                                id:'select',
                                iconCls:'icn-search',
                                xtype:'button',
            					text:'选择选中',
                                handler:find_select
                                },{
                                minWidth:80,
                                id:'search',
                                iconCls:'icn-search',
                                xtype:'button',
            					text:'检查',
                                handler:search_submit
                                },{
                                minWidth:80,
                                id:'submit',
                                iconCls:'icn-forward',
                                xtype:'button',
            					text:'确定',
                                disabled:true,
                                handler:update_submit
                                }] }                           			 
								 ]
	});			
 
function find_select(){
 	var selectRows=grid.getSelectionModel().getSelections();    	
        if(selectRows.length>0){
            for(var i=0;i<selectRows.length;i++){								
														var ids=Ext.getCmp('product_id').getValue();									
                                                        var id=	selectRows[i].get(dataId);
                                                        if(ids.indexOf(id)<0){	
                                                        if(ids==""){
                                                                Ext.getCmp('product_id').setValue(id);
                                                                }
                                                                else{
                                                                Ext.getCmp('product_id').setValue(ids+","+id);
                                                                }
                  										}
				}
    }
 }
function search_submit(){
	var type=Ext.getCmp('type').getGroupValue();
	var ids=Ext.getCmp('product_id').getValue();   
    var typePattern0=/^\d[0-9,]+\d$/;//只允许数字和逗号，且开头和结尾必须为数字
    var typePattern1=/^\d[0-9,-]+\d$/;//只允许数字。逗号和连线。且开头和结尾必须为数字
    //ds.load({params:{start:0,limit:60,where:"id="+ids}});
    if(type==0){
    //检查输入ID号是否满足正则
    	if(!typePattern0.test(ids)){
        	Ext.MessageBox.alert("error","ID号不满足规则:只允许数字和逗号，且开头和结尾必须为数字");
        	return;
        }
    }
    if(type==1){
    	if(!typePattern1.test(ids)){
        	Ext.MessageBox.alert("error","ID号不满足规则:只允许数字。逗号和连线。且开头和结尾必须为数字");
        	return;
        }
    }
    ds.load({params:{start:0,limit:100,ids:ids,type:type}}); 
	Ext.getCmp("submit").enable();
}
function  update_submit(){
        var typeValue=Ext.getCmp("type").getGroupValue();
        var siteid=Ext.getCmp('site_id').getValue();
        var ids=Ext.getCmp('product_id').getValue(); 
        if(siteid==""){
        	Ext.MessageBox.alert("提示","未选择目标站点");
            return;
        }
        if(ids==""){
        	Ext.MessageBox.alert("提示","产品ID为空");
            return;
        }		  
		Ext.Ajax.request({
                 url:menuDelUrl,
                 params:{id:ids,type:typeValue,siteid:siteid},																																		 				success:function(response,option){
            		var temp=Ext.util.JSON.decode(response.responseText);																
            		ds.load({params:{start:0,limit:30}});
            		grid.render();
                },
                failure:function(response,option){
                    var temp=Ext.util.JSON.decode(response.responseText);
                    Ext.MessageBox.alert('waring',temp.msg);
                }
        });
}		
      
<?php $gridId=uniqid(); ?>
//每页显示记录数
var mypagedata=[
				['10','10'],
				 ['15','15'],
				 ['20','20'],
				 ['40','40'],
				 ['200','200']
				 ];	
var page_size_store=new Ext.data.SimpleStore({
									   fields:[
											   {name:'id'},
											   {name:'name'},
									   		  ]
									   });
page_size_store.loadData(mypagedata);
var pagesize_combo=new Ext.form.ComboBox({
										 store:page_size_store,
										 width:50,										
										 emptyText:'10',
										 mode:'local',
										 triggerAction:'all',
										 valueField:'name',
										 displayField:'id'
										 });
pagesize_combo.on('select',function(e){										
										var pagesize=parseInt(e.getValue());
										pagBar.pageSize=pagesize;
										ds.load({params:{start:0,limit:pagesize}});
										});	

//定义分页条栏				 
var pagBar=new Ext.PagingToolbar({
										pageSize:30,
										store:ds,
										displayInfo:true,
										displayMsg:'第{0}到{1}条记录 共{2}条记录',
										emptyMsg:'没有数据记录',
										//plugins: filters,
										items:['-','每页显示',pagesize_combo,'条记录','-']
									 });                          
//定义容器
var grid=new Ext.grid.EditorGridPanel({
									renderTo:'grid_<?=$gridId?>',
									store:ds,
									cm:cm,									
									height:500,
									minHeight:300,
									autoWidht:true,								
									loadMask:{msg:'加载服务端数据中',store:ds},	
									autoScroll:true,
									stripeRows:true,									
        							animCollapse: false,
									monitorWindowResize: true,	
									title:gridTitle,
									clicksToEdit:true,                                   
									tbar:tb,
                                    sm:new Ext.grid.CheckboxSelectionModel({singleSelect:false}),
									bbar:pagBar,                                    
                                    stateful:true,  
									stateId:'update_'+module
									});
//显示数据
ds.load({params:{start:0,limit:30}});
grid.render();


function menuDel(){												
												var selectRows=grid.getSelectionModel().getSelections();												
												if(selectRows.length==0){
													Ext.MessageBox.alert('提示','请选择一条数据');
													}
												else{		
													var message="";
													for(var i=0;i<selectRows.length;i++){
														if(i!=0){
															message+=",";
															}
														message+=selectRows[i].get(dataId);
														}
													Ext.Ajax.request({
																	 url:menuDelUrl,
																	 params:{id:message},																																		 																	success:function(response,option){
																var temp=Ext.util.JSON.decode(response.responseText);																
																ds.load({params:{start:0,limit:30}});
																grid.render();
																		 },
																	failure:function(response,option){
																		var temp=Ext.util.JSON.decode(response.responseText);
																		Ext.MessageBox.alert('waring',temp.msg);
																		}
																	 });
													}
												}

function menuSave(){
	afterEditSave();
	Ext.getCmp("menu_save"+ExtId).setDisabled(true);
}

function afterEditSave(){
	var　modified　=　ds.modified;
	var json=[];
	Ext.each(modified,function(field){
						json.push(field.data);
						});
	if(json.length>0){
		Ext.Ajax.request({
						 url		: menuEditUrl,
						 params		: {data:Ext.util.JSON.encode(json)},
						 method		: 'POST',
						 success	: function(response){
							 	Ext.Msg.alert('信息','数据更新成功',function(){																	
										Ext.getCmp('content').getActiveTab().getUpdater().refresh();																
								});
							 }
						 });
		}
	else{
		Ext.Msg.alert("信息","没有修改任何信息");
		}
	//alert(e.field);
	//alert(e.record.get('role'));
}
 
});
function show_link(val){
	return "<a href='<?=$this->website?>/"+val+"' target='_blank'>"+val+"</a>";
}
function show_money(val){
	if(parseInt(val)===0 || val===null){
		return "<span style='color:red'>"+val+"</span>";
	}
	return "$"+val+" USD";
}
function show_img(val){
	//return "<span style='display:table' qtip='<img src=\'<?=$this->website?>/media/catalog/product/"+val+"\''>"+val+"</span>";	
	var link='<?=$this->website?>/media/catalog/product/'+val;
	return "<a href='"+link+"' class='lb-flower'><img src='"+link+"' width='50' height='30' /></a>";
}
</script>
<body style="width:98%;height:100%">
<div id='grid_<?=$gridId?>'></div>
<div id='message'></div>
</body>
</html>

